package com.sss.gsb.client.proxy;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sss.gsb.client.grid.GridDataModel;
import com.sss.gsb.client.grid.RowModel;
import com.sss.gsb.message.schemas.BondsManRequest;
import com.sss.gsb.message.schemas.BondsManResponse;
import com.sss.gsb.message.schemas.BondsmanType;
import com.sss.gsb.message.schemas.DeadmanRequest;
import com.sss.gsb.message.schemas.DeadmanResponse;
import com.sss.gsb.message.schemas.DeadmanType;
import com.sss.gsb.message.schemas.InsuranceRequest;
import com.sss.gsb.message.schemas.InsuranceResponse;
import com.sss.gsb.message.schemas.InsuranceType;
import com.sss.gsb.message.schemas.LoanRequest;
import com.sss.gsb.message.schemas.LoanResponse;
import com.sss.gsb.message.schemas.LoanType;
import com.sss.gsb.message.schemas.ObjectFactory;
import com.sss.gsb.service.LoadParameter;
import com.sss.gsb.util.ClassPathResourceUtil;
import com.sss.gsb.util.SchemaConvertionUtils;

@Service
public class GSBWebServiceProxy extends AbstractSSLClient implements
		GSBClient {

	
	private static final Logger log = LoggerFactory
			.getLogger(GSBWebServiceProxy.class);

	/**
	 * @return
	 */
	private ObjectFactory getObjectFactory() {
		ObjectFactory factory = new ObjectFactory();
		return factory;
	}


	@Override
	public File getKeyStoreFile() {
		try {
			return ClassPathResourceUtil.getFileFromResource("classpath:keystore/transport/ssl/client.jks");
		} catch (IOException e) {
			log.error("[ Keystore File exception ]", e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String getKeyStorePass() {
		return "clientstore";
	}

	@Override
	public File getTrustStoreFile() {
		try {
			return ClassPathResourceUtil.getFileFromResource("classpath:keystore/transport/ssl/truststore.jks");
		} catch (IOException e) {
			log.error("[ TrustStore File exception ]", e.getMessage());
		}
		return null;
	}

	@Override
	public String getTrustStorePass() {
		return "truststore";
	}

	@Override
	public GridDataModel<RowModel> loadBondsMan(LoadParameter param)
			throws Exception {
		BondsManRequest request = getObjectFactory().createBondsManRequest();
//		request.setId(param.getId());		
		request.setCpkCode(param.getCpk());
		request.setCpsCode(param.getCps());
		request.setIdCard(param.getIdCard());
		request.setName(param.getName());
		request.setSurname(param.getSurname());
		
		GridDataModel<RowModel> model = new GridDataModel<RowModel>();
		try{
			BondsManResponse response = (BondsManResponse)this.performWebServiceTemplate()
				.marshalSendAndReceive(request);
			log.info("load bondsman ws result size : {}",response.getBondsman().size());
			for(BondsmanType m : response.getBondsman()){
				model.addData(SchemaConvertionUtils.toGridModel(model.getTotal(),m));
			}
			return model;
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}	
	}

	@Override
	public GridDataModel<RowModel> loadDeadMan(LoadParameter param)
			throws Exception {
		DeadmanRequest request = getObjectFactory().createDeadmanRequest();
//		request.setId(param.getId());
		request.setCpkCode(param.getCpk());
		request.setCpsCode(param.getCps());
		request.setIdCard(param.getIdCard());
//		log.info("");
		request.setName(param.getFullName());
		request.setSurname(param.getSurname());
		GridDataModel<RowModel> model = new GridDataModel<RowModel>();
		try{
			DeadmanResponse response = (DeadmanResponse)this.performWebServiceTemplate()
				.marshalSendAndReceive(request);
			log.info("load dead man ws result size : {}",response.getDeadman().size());
			for(DeadmanType m:response.getDeadman()){
				model.addData(SchemaConvertionUtils.toGridModel(model.getTotal(),m));
			}
			return model;
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}	
	}

	@Override
	public GridDataModel<RowModel> loadInsurance(LoadParameter param)
			throws Exception {
		InsuranceRequest request = getObjectFactory().createInsuranceRequest();
//		request.setId(param.getId());		
		request.setCpkCode(param.getCpk());
		request.setCpsCode(param.getCps());
		request.setIdCard(param.getIdCard());
		request.setName(param.getName());
		request.setSurname(param.getSurname());
		GridDataModel<RowModel> model = new GridDataModel<RowModel>();
		try{
			InsuranceResponse response = (InsuranceResponse)this.performWebServiceTemplate()
				.marshalSendAndReceive(request);
			log.info("load insurance ws result size : {}",response.getInsurance().size());
			for(InsuranceType m:response.getInsurance()){
				model.addData(SchemaConvertionUtils.toGridModel(model.getTotal(),m));
			}
			return model;
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}	
	}

	@Override
	public GridDataModel<RowModel> loadLoan(LoadParameter param) throws Exception {
		LoanRequest request = getObjectFactory().createLoanRequest();
//		request.setId(param.getId());
		request.setCpkCode(param.getCpk());
		request.setCpsCode(param.getCps());
		request.setIdCard(param.getIdCard());
		request.setName(param.getName());
		request.setSurname(param.getSurname());
		GridDataModel<RowModel> model = new GridDataModel<RowModel>();
		try{
			LoanResponse response = (LoanResponse)this.performWebServiceTemplate()
				.marshalSendAndReceive(request);
			log.info("load loan ws result size : {}",response.getLoan().size());
			for(LoanType m:response.getLoan()){
				model.addData(SchemaConvertionUtils.toGridModel(model.getTotal(),m));
			}
			return model;
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}	
	}

	@Override
	public GridDataModel<RowModel> loadLoanAccountClosed(
			LoadParameter param) throws Exception {
		throw new UnsupportedOperationException("has been changed to load Loan account instead");
	
	}

}
